<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>购物车</title>
    <link rel="stylesheet" href="/style/weui.css"/>
    <link rel="stylesheet" href="/style/swiper.min.css"/>
    <link rel="stylesheet" href="/style/example.css"/>
    <link rel="stylesheet" href="/style/sweetalert.css"/>
</head>
<body ontouchstart>
<div class="page">
    <div class="page__bd" style="height: 100%;">
        <div class="weui-tab">
            <div class="weui-tab__panel">
                <!-- 收货信息 -->
                <div class="weui-cells first-cells">
                <div class="weui-cells__title">
                    门店信息
                </div>
                <a class="weui-cell weui-cell_access" href="javascript:;" id="showPicker">
                    <div class="weui-cell__bd">
                        <div style="float: left">门店：<span id="shopName"></span></div>
                        <div style="float: right" id="shopAdd"></div>
                        <input type="hidden" id="shopId" value="">
                    </div>
                    <div class="weui-cell__ft">
                    </div>
                </a>
            </div>
            <!-- 收货信息 end-->
                <div id="div_warn" class="weui-cells" style="color: red;">
                </div>
            <!-- 商品信息 -->
            <div class="weui-cells top-5-cells">

                <div class="weui-cell">
                    <div class="weui-cell__bd text-center">
                        <h4>商品信息</h4>
                    </div>
                </div>
                <div id="div_goods">
                    <!-- list -->
                    <?php $total=0;$totalPrice=0; if($goods){ foreach ($goods as $g) {?>
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <?=$g['materielName'] ?>
                            </div>
                            <div class="weui-cell__bd text-center">
                                <?=$g['specDesc'] ?>
                            </div>
                            <div class="weui-cell__bd text-center">
                              <?php if($shopType == 1) {?> <?=$g['salePrice'] ?> <?php } ?>  X <?=$g['amount'] ?>
                            </div>
                        </div>
                    <?php $total += $g['amount']; $totalPrice+= $g['amount']*$g['salePrice']?>
                    <?php } } ?>
                    <!-- list end-->
                    <!-- list collect-->

                    <div class="weui-cell">
                        <div class="weui-cell__bd text-center">
                            共 <?=$total?> 件商品<?php if($shopType == 1) {?>,商品总价 <?=$totalPrice ?> 元, 运费: <?=$deliveryFee?> 元 <?php } ?>
                        </div>
                    </div>

                    <?php if($shopType == 1) {?>
                        <div class="weui-cell">
                            <div class="weui-cell__bd text-center">
                                <?php $totalp = $totalPrice+$deliveryFee; ?>
                                待支付金额：<span style="color: red;font-size: 14px"><?=$totalp?> </span>
                            </div>
                        </div>
                    <?php } ?>


                </div>
                <!-- list collect end-->
            </div>
            <!-- 商品信息 end-->
        </div>
        <!-- tabbar -->
        <div class="weui-tabbar">
            <div class="weui-btn-box">

                <?php if($shopType == 1) {?>
                    <a href="javascript:h5Alipay();" class="weui-btn weui-btn_warn" id="declareDialog">
                        去支付<span style="font-size: 12px">(请15分钟内完成付款)<span>
                    </a>
                <?php } else { ?>
                    <a href="javascript:submit();" class="weui-btn weui-btn_warn" id="declareDialog">
                        提交订单
                    </a>
                <?php } ?>
            </div>
        </div>
        <!-- tabbar end-->
    </div>
</div>
</div>

<script src="/js/zepto.min.js"></script>
<script src="/js/sweetalert.min.js"></script>
<script src="/js/swiper.min.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.0.0/weui.min.js"></script>
<script>

    var shopData= <?=$shop ?>;
    $(function () {
        var select = shopData[0];
        $('#shopId').val(select.id);
        $('#shopName').html(select.label);
        $('#shopAdd').html(select.info);

        if(shopData.length > 1) {
            $('#showPicker').on('click', function () {
                var _this = $(this);
                weui.picker(shopData, {
                    onChange: function (result) {
                        //console.log(result);
                    },
                    onConfirm: function (result) {
                        var select = shopData[result[0]];
                        $('#shopId').val(select.id);
                        $('#shopName').html(select.label);
                        $('#shopAdd').html(select.info);

                        viewHandle.clear();
                        $.post('/order/cart-goods-by-shop-id', {'shopId': select.id}, function (o) {
                            if(o.code == 200){
                                viewHandle.showGoods(o.data);
                            } else {
                                swal('系统错误,请重试！');
                            }
                        },'json');
                        <?php if($shopType == 1) {?>
                        checkShopLimit(select.id);
                        <?php } ?>
                    }
                });
            });
        }

        <?php if($shopType == 1) {?>
        checkShopLimit(select.id);
        <?php } ?>
    });

    function checkShopLimit(shopid) {
        $.post('/order/check-shop-limit', {'shopId': shopid}, function (o) {
            if (o.code != 200) {
                viewHandle.showWarn(o.data);
            }else{
                viewHandle.clearWarn();
            }
        }, 'json')

    }




    function h5Alipay() {
        submit_pre('h5alipay');
    }

    // function h5Wechat() {
    //     submit_pre('h5wxpay');
    // }


    function submit_pre(platform) {

        var shopId = $('#shopId').val();
        if(!shopId){
            swal('请选择门店信息');
            return;
        }
        swal({
            title: $('#shopName').html(),
            text: '确认门店信息',
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            showCancelButton: true,
            closeOnConfirm: false
        }, function (flg) {
            if (flg) {
                swal('订单提交中...');
                var url = '/order/submit-pre';
                $.post(url, {'shopId': shopId,'platform':platform}, function (o) {
                    console.log(o);
                    if (o.code == 200) {
                        location.href = o.data.url;
                    } else {
                        swal(o.msg);
                        if (o.code == 501) {
                            setTimeout(function () {
                                location.href = '/cart';
                            }, 1500);
                        }
                    }
                },'json')
            }
        });

    }


    function submit() {
        var shopId = $('#shopId').val();
        if(!shopId){
            swal('请选择门店信息');
            return;
        }
        swal({
            title: $('#shopName').html(),
            text: '确认门店信息',
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            showCancelButton: true,
            closeOnConfirm: false
        }, function (flg) {
            if (flg) {
                var url = <?php if($shopType == 1) {?>'/order/submit-pre'<?php } else {?>'/order/submit'<?php }?>;
                $.post(url, {'shopId': shopId}, function (o) {
                    if (o.code == 200) {
                        swal('提交成功');
                        setTimeout(function () {
                            location.href = '/order/list';
                        }, 1000);
                    } else {
                        swal(o.msg);
                        if (o.code == 501) {
                            setTimeout(function () {
                                location.href = '/cart';
                            }, 1500);
                        }
                    }
                },'json')
            }
            //swal.close();
        });

    }

    var viewHandle = {
        showWarn:function(warns){
            $("#div_warn").empty();
            var html = '';
            $.each(warns,function (i,msg) {
                html += '<div class="weui-cell">'+msg+'</div>';
            });
            $("#div_warn").append(html);

        },
        clearWarn:function(){
            $("#div_warn").empty();
        },
        showGoods: function (data) {
            var total = 0;
            var totalPrice = 0;
            var goods = data.goods;
            $.each(goods, function (i, good) {
                total += parseInt(good.amount);
                totalPrice += (parseInt(good.amount) * parseFloat(good.salePrice));
                var html = '<div class="weui-cell">';
                html += '        <div class="weui-cell__bd">' + good.materielName + '</div>';
                html += '        <div class="weui-cell__bd text-center">' + good.specDesc + '</div>';


                var salePrice = '';
                <?php if($shopType == 1) {?>
                salePrice = good.salePrice;
                <?php } ?>

                html += '        <div class="weui-cell__bd text-center">' + salePrice + ' X ' + good.amount + '</div>';


                html += '   </div>';
                $("#div_goods").append(html);
            });

            var htmltotal = '<div class="weui-cell">';

            var totalPriceDesc = '';
            <?php if($shopType == 1) {?>
            totalPriceDesc = ',商品总价 ' + totalPrice.toFixed(4) + ' 元, 运费:'+ data.deliveryFee.toFixed(2) + '元';
            <?php } ?>
            htmltotal += '<div class="weui-cell__bd text-center">共 ' + total + ' 件商品' + totalPriceDesc + '</div>';
            htmltotal += '</div>';
            $("#div_goods").append(htmltotal);

            <?php if($shopType == 1) {?>
            var htmlpayprice = '<div class="weui-cell">';
            htmlpayprice += '<div class="weui-cell__bd text-center">待支付金额：<span style="color: red;font-size: 14px"> ' + (totalPrice +data.deliveryFee).toFixed(2) + '</span></div>';
            htmlpayprice += '</div>';
            $("#div_goods").append(htmlpayprice);
            <?php } ?>
        },

        clear: function () {
            $("#div_goods").empty();
        }
    }
</script>
</body>
</html>
